package com.example.springsecurity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.Echart;
import com.example.springsecurity.mapper.ProjectCountEchartsMapper;
import com.example.springsecurity.mapper.ProjectTeachMapper;
import com.example.springsecurity.service.ProjectCountEchartsService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.example.springsecurity.utils.redis.RedisCache;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName ProjectCountEchartsServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-10-20 9:33
 **/
@Service
public class ProjectCountEchartsServiceImpl implements ProjectCountEchartsService {

    @Autowired
    private ProjectCountEchartsMapper projectCountEchartsMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ProjectTeachMapper projectTeachMapper;

    /**
     * 获取学院实验分布
     * @return
     */
    @Override
    public List<Map<String,Object>> getCollegeDistribute() {
        return projectCountEchartsMapper.getCollegeDistribute();
    }

    /**
     * 获取项目专业分布
     * @return
     */
    @Override
    public List<Map<String, Object>> getMajorDistribute() {
        return projectCountEchartsMapper.getMajorDistribute();
    }

    /**
     * 获取校内教学实验布置轮次
     * @param schoolYear
     * @return
     */
    @Override
    public List<Map<String, Object>> getAssignCount(String schoolYear) {
        return projectCountEchartsMapper.getAssignCount(schoolYear,null);
    }

    /**
     * 获取校内教学学生使用人次
     * @param schoolYear
     * @return
     */
    @Override
    public List<Map<String, Object>> getAssignStudentCount(String schoolYear) {
        return projectCountEchartsMapper.getAssignStudentCount(schoolYear,null);
    }

    /**
     * 获取所有项目
     * @param kind
     * @return
     */
    @Override
    public List<Echart> getAllProject(Integer kind) {
        return projectCountEchartsMapper.getAllProject(kind);
    }

    /**
     * 获取共享统计点击量
     * @param ranking 排名   例如：top3,top7
     * @return
     */
    @Override
    public List<Echart> getClickCount(Integer ranking) {
        /**
         * 获取所有项目信息
         */
        List<Echart> projects = projectCountEchartsMapper.getAllProject(1);
        /**
         * 获取redis中存放的数据
         */
        for(Echart project:projects){
            Integer projectId = project.getProjectId();
            //获取token中存放的点击量   浏览量
            Integer count = redisCache.getCacheObject("searchExperiment:"+projectId);
            if(StringUtils.isNull(count) || count == null){
                count = 0;
            }
            project.setCount(count);
        }
        /**
         * 对集合进行排序，排序依据是对象的count
         */
        List<Echart> projects1 = projects.stream().sorted(Comparator.comparing(Echart::getCount).reversed()).collect(Collectors.toList());
        if(!StringUtils.isNull(ranking) || ranking != null){
            if(projects1.size() >= ranking){
                return projects1.subList(0,ranking);
            }else{
                return projects1;
            }
        }else{
            return projects1;
        }
    }

    /**
     * 获取共享统计学习人次
     * @param ranking 排名   例如：top3,top7
     * @return
     */
    @Override
    public List<Echart> getStudyCount(Integer ranking,Date time) {
        /**
         * 获取所有项目信息
         */
        List<Echart> projects = projectCountEchartsMapper.getAllProject(1);
        List<Echart> projects2 = projectCountEchartsMapper.getStudyCount(ranking,time,projects);
        //判断在时间后根本没有人做实验
        if(StringUtils.isNull(projects2) || projects2.size()==0){
            if(StringUtils.isNull(ranking) || ranking == null){
                return projects;
            }
            return projects.subList(0,ranking);
        }
        return projects2;
    }

    /**
     * 获取共享统计完成人次
     * @param ranking
     * @param time
     * @return
     */
    @Override
    public List<Echart> getFinishCount(Integer ranking, Date time) {
        List<Echart> projects = projectCountEchartsMapper.getFinishCount(ranking,time,null);
        if(StringUtils.isNull(projects) || projects.size() == 0){
            /**
             * 获取所有项目信息
             */
            List<Echart> projects2 = projectCountEchartsMapper.getAllProject(1);
            if(StringUtils.isNull(ranking) || ranking == null){
                return projects2;
            }else if(projects2.size() < ranking){
                return projects2;
            }
            return projects2.subList(0,ranking);
        }
        return projects;
    }

    /**
     * 获取共享统计学习时长
     * @param ranking
     * @return
     */
    @Override
    public List<Echart> getStudyTime(Integer ranking,Date time) {
        List<Echart> projects = projectCountEchartsMapper.getStudyTime(ranking,null,time);
        if(StringUtils.isNull(projects) || projects.size() == 0){
            /**
             * 获取所有项目信息
             */
            List<Echart> projects2 = projectCountEchartsMapper.getAllProject(1);
            if(StringUtils.isNull(ranking) || ranking == null){
                return projects2;
            }else if(projects2.size() < ranking){
                return projects2;
            }
            return projects2.subList(0,ranking);
        }
        return projects;
    }

    /**
     * 获取在共享统计大目录下，某一个实验的成绩分布
     * @param projectId
     * @return
     */
    @Override
    public List<Map<String, Object>> getScoreDistributeByShare(Integer projectId) {
        return projectCountEchartsMapper.getScoreDistributeByShare(projectId);
    }

    /**
     * 获取校内教学下的某一个实验成绩分布
     * @param projectId
     * @return
     */
    @Override
    public List<Map<String, Object>> getScoreDistributeAll(Integer projectId,String time) {
        return projectCountEchartsMapper.getScoreDistributeAll(projectId,time);
    }


    /**
     * 校内教学成绩分布  (获取当前学年该实验是否布置)
     * @param projectId
     * @param time
     * @return
     */
    @Override
    public Integer getAssignCountById(Integer projectId, String time) {
        return projectCountEchartsMapper.getAssignCountById(projectId,time);
    }



    /**
     * 获取详细数据  (校内教学大目录下)
     * @param pageNum
     * @param pageSize
     * @param time
     * @return
     */
    @Override
    public ResultBean getDetailDataAll(Integer pageNum, Integer pageSize, String time, List<Echart> echartList) {
        long count = 0;
        /**
         * 设置分页
         */
        List<Echart> echarts = null;
        if(!StringUtils.isNull(echartList) || echartList != null){
            echarts = echartList;
            count = echartList.size();
        }else{
            PageHelper.startPage(pageNum,pageSize);
            echarts = projectCountEchartsMapper.getAllProject(null);
            PageInfo<Echart> pageInfo = new PageInfo<>(echarts);
            count = pageInfo.getTotal();
        }
        /**
         * 设置json集合传json数据
         */
        List<JSONObject> jsonObjects =new ArrayList<>();
        for(Echart echart:echarts){
            Integer projectId = echart.getProjectId();
            JSONObject jsonObject = new JSONObject();
            /**
             * 设置基本信息
             */
            jsonObject.put("projectName",echart.getProjectName());
            jsonObject.put("collegeName",echart.getCollegeName());
            jsonObject.put("majorName",echart.getMajorName());
            jsonObject.put("userName",echart.getUserName());
            /**
             * 设置布置人数和布置次数
             */
            List<Map<String,Object>> list = projectCountEchartsMapper.getAssignCount(time,projectId);
            if(StringUtils.isNull(list) || list.size()==0){
                jsonObject.put("assignCount",0);
            }else{
                jsonObject.put("assignCount",list.get(0).get("count"));
            }
            List<Map<String,Object>> list2 = projectCountEchartsMapper.getAssignStudentCount(time,projectId);
            if(StringUtils.isNull(list2) || list2.size()==0){
                jsonObject.put("assignStudentCount",0);
            }else{
                jsonObject.put("assignStudentCount",list2.get(0).get("count"));
            }
            /**
             * 设置已批改，已提交，未提交人数
             */
            jsonObject.put("corrected",projectCountEchartsMapper.getCorrected(projectId,time));
            jsonObject.put("submitted",projectCountEchartsMapper.getSubmitted(projectId,time));
            jsonObject.put("unSubmitted",projectCountEchartsMapper.getUnSubmitted(projectId,time));
            /**
             * 获取其优秀人数,及格人数
             */
            jsonObject.put("excellent",projectCountEchartsMapper.getExcellent(projectId,time));
            jsonObject.put("pass",projectCountEchartsMapper.getPass(projectId,time));
            //将其加入到集合中
            jsonObjects.add(jsonObject);
        }
        ResultBean resultBean = new ResultBean();
        resultBean.put("code",200);
        resultBean.put("msg","获取数据成功");
        resultBean.put("total",count);
        resultBean.put("data",jsonObjects);
        return resultBean;
    }


    /**
     * 获取详细数据  (共享统计下)
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultBean getDetailDataShare(Integer pageNum, Integer pageSize, List<Echart> echartList) {
        long count = 0;
        /**
         * 设置分页
         */
        List<Echart> echarts = null;
        if(!StringUtils.isNull(echartList) || echartList != null){
            echarts = echartList;
            count = echartList.size();
        }else{
            PageHelper.startPage(pageNum,pageSize);
            echarts = projectCountEchartsMapper.getAllProject(1);
            PageInfo<Echart> pageInfo = new PageInfo<>(echarts);
            //System.out.println(pageInfo);
            count = pageInfo.getTotal();
        }
        /**
         * 设置json集合传json数据
         */
        List<JSONObject> jsonObjects =new ArrayList<>();
        for(Echart echart:echarts){
            Integer projectId = echart.getProjectId();
            JSONObject jsonObject = new JSONObject();
            /**
             * 设置基本信息
             */
            jsonObject.put("projectName",echart.getProjectName());
            jsonObject.put("collegeName",echart.getCollegeName());
            jsonObject.put("majorName",echart.getMajorName());
            jsonObject.put("userName",echart.getUserName());
            /**
             * 设置点击量和访问人次
             */
            if(StringUtils.isNull(redisCache.getCacheObject("searchExperiment:"+projectId))){
                jsonObject.put("clickCount",0);
            }else{
                jsonObject.put("clickCount",redisCache.getCacheObject("searchExperiment:"+projectId));
            }
            if(StringUtils.isNull(redisCache.getCacheObject("doExperiment:"+projectId))){
                jsonObject.put("visitCount",0);
            }else {
                jsonObject.put("visitCount",redisCache.getCacheObject("doExperiment:"+projectId));
            }
            /**
             * 设置完成人次和学时
             */
            List<Echart> list = projectCountEchartsMapper.getFinishCount(null,null,projectId);
            if(StringUtils.isNull(list) || list.size()==0){
                jsonObject.put("finishCount",0);
            }else{
                jsonObject.put("finishCount",list.get(0).getCount());
            }
            List<Echart> list2 = projectCountEchartsMapper.getStudyTime(null,projectId,null);
            if(StringUtils.isNull(list2) || list2.size()==0){
                jsonObject.put("studyTime",0);
            }else{
                if(StringUtils.isNull(list2.get(0).getCount())){
                    jsonObject.put("studyTime",0);
                }else{
                    jsonObject.put("studyTime",list2.get(0).getCount());
                }
            }
            /**
             * 设置已批改，优秀，及格人数
             */
            jsonObject.put("corrected",projectCountEchartsMapper.getCorrected2(projectId));
            jsonObject.put("excellent",projectCountEchartsMapper.getExcellent2(projectId));
            jsonObject.put("pass",projectCountEchartsMapper.getPass2(projectId));
            jsonObjects.add(jsonObject);
        }
        ResultBean resultBean = new ResultBean();
        resultBean.put("code",200);
        resultBean.put("msg","获取数据成功");
        resultBean.put("total",count);
        resultBean.put("data",jsonObjects);
        return resultBean;
    }

    /**
     * 获取所有布置的人次
     * @return
     */
    @Override
    public Integer getAllAssign() {
        return projectCountEchartsMapper.getAllAssign();
    }

    /**
     * 获取所有共享体验人次
     * @return
     */
    @Override
    public Integer getAllShare() {
        return projectCountEchartsMapper.getAllShare();
    }

    /**
     * 必修学习时间
     * @return
     */
    @Override
    public Double getAllAssignHour() {
        return projectCountEchartsMapper.getAllAssignHour();
    }

    /**
     * 共享实验学习时间
     * @return
     */
    @Override
    public Double getAllShareHour() {
        return projectCountEchartsMapper.getAllShareHour();
    }

    /**
     * 注册人数
     * @return
     */
    @Override
    public Integer getAllRegister() {
        return projectCountEchartsMapper.getAllRegister();
    }


    /**
     * 获取用户负责的实验项目
     * @return
     */
    @Override
    public List<Echart> getProjects(Long userId) {
        /**
         * 获取教师负责的所有项目id
         */
        List<Integer> lists = projectTeachMapper.getProjectIdsByUserId(userId);
        return projectCountEchartsMapper.getProjects(lists);
    }


    /**
     * 获取用户负责的共享实验项目
     * @return
     */
    @Override
    public List<Echart> getProjectsShare(Long userId) {
        return projectCountEchartsMapper.getProjectsShare(userId);
    }
}
